Git筆記 - 在本地端和Github傳遞repo


觀念

(1) 將本地檔案藉由git上傳至github

一開始本地端folder要先以git init進行初始化

將本地端要上傳的文件用git add file放入staging area

將staging area中要存入repo的檔案以git commit file處理

將本地repo的內容推到github的repo git push

(2) 將github的repo檔案下載至本地端 git pull

當然還有將remote branch傳至本地端branch的
git fetch
git pull

整體流程:

(3) branch

練習

git add / git commit

1.首先建立一個folder在vscode打開
並且使用git init來初始化repo

2.增加file - file1.txt, 輸入git status


表示目前branch在 master
尚未有commit的file (repo為空)
Untracked file紅字表示有未加入到staging的new file 或是file modify

3.將file家入staging git add file1.txt
再使用git status

綠色表示有把new file / file modify存到staging中

4.將staging 全部file放入本地repo git commit -m "first to commit"
(直接使用git commit會進到編輯模式 需要用到:wq出去)

查看狀況git status

mothing to commit --> 中沒有東西等著被commit
working tree clean --> 沒有東西等者被放進staging

查看log git log

push / branch 將本地repo放到遠端repo

當我們要將本地端的repo傳遞到github repo時,這時對我們來說就是傳本地branch到一個 remote branch
而這個遠端的branch URL我們可以用name來代替
(本地端通常是 main/master)
git remote add [name] https://...

額外語法:
git remote rename [old] [new]
git remote remove [name]

1.首先在github創同名的repo Learn_git,並找到以下指令

2.git remote -v 查看有無remote branch --> 沒東西
git remote add origin https://github.com/LTN917/Learn_git.git
將remote branch https://... 以origin代替

3.把本地branch push 到 remote branch
git push -u origin main (local branch為main)
--> 將local branch main推到remote branch origin

4.到github查看push成果

fetch / pull 將遠端repo放到本地repo

git fetch 將remote repo 傳至 local repo
但是不會複寫現在的 local branch,而是保留在編輯的階段
(下載新的也保留原本的)

git pull 將remote repo 傳至 local repo
而且會複寫現在的 local branch
--> merge local branch with remote branch
(下載新的更新原本的)

1.先去github repo上建立new file newfile.txt
git fetch

(顯示從branch origin fetch資料到 branch main)
但是新檔案newfile.txt沒被加入工作區

2.用檢查的方式看一下origin/main之間到底fetch了甚麼
git checkout origin/main

檔案也有顯示在工作區但並沒有真正載入,只是可以檢視

git switch main 轉回到local branch
這時newfile.txt就不能在讀取

3.git pull將remote repo真正寫入local repo
使其拉到工作區也可以使用跟改寫








你可能感興趣的文章

遠端主機部署心得筆記

遠端主機部署心得筆記

Remove Duplicates from an Array (ES6)

Remove Duplicates from an Array (ES6)

ASP.NET Core Web API 入門教學 - AuthorizationFilter

ASP.NET Core Web API 入門教學 - AuthorizationFilter






留言討論